package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.bo.ModulesBO;
import com.junzhiit.nengji.dao.ModulesDAO;
import com.junzhiit.nengji.model.Modules;

@Repository
public class ModulesDAOImpl extends AbstractBaseDAO<Modules> implements ModulesDAO {

	@Override
	public List<ModulesBO> findAll(Modules modules, int pageNumber, int maxPageRow) {

		StringBuffer sql = new StringBuffer();
		sql.append(
				" select m.id,m.code,m.name,m.column_id,m.goods_quantity,m.sort,m.is_recommand_goods,c.name column_name from modules m");
		sql.append(" left join `column` c on m.column_id=c.id");
		sql.append(" where m.is_delete=?");

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		if (StringUtils.isNotBlank(modules.getName())) {
			sql.append(" and m.name like ?");
			param.setString("%" + modules.getName() + "%");
		}

		if (StringUtils.isNotBlank(modules.getColumnId())) {
			sql.append(" and m.column_id=?");
			param.setString(modules.getColumnId());
		}
		
		if (StringUtils.isNotBlank(modules.getShopId())) {
			sql.append(" and m.shop_id= ? ");
			param.setString(modules.getShopId());
		}else{
			sql.append(" and m.shop_id is null ");
		}

		if(modules.getIsOpenPage()!=null){
			sql.append(" and m.is_open_page=?");
			param.setInt(modules.getIsOpenPage());;
		}
		sql.append(" order by m.code,m.sort asc");

		return super.queryForPagingList(sql.toString(), ModulesBO.class, param, pageNumber, maxPageRow);
	}

	@Override
	public int getTotalCount(Modules modules) {

		StringBuffer sql = new StringBuffer();
		sql.append(" select count(m.id) from modules m");
		sql.append(" left join `column` c on m.column_id=c.id");
		sql.append(" where m.is_delete=?");

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		if (StringUtils.isNotBlank(modules.getName())) {
			sql.append(" and m.name like ?");
			param.setString("%" + modules.getName() + "%");
		}

		if (StringUtils.isNotBlank(modules.getColumnId())) {
			sql.append(" and m.column_id=?");
			param.setString(modules.getColumnId());
		}
		
		if (StringUtils.isNotBlank(modules.getShopId())) {
			sql.append(" and m.shop_id=?");
			param.setString(modules.getShopId());
		}else{
			sql.append(" and m.shop_id is null ");
		}

		if(modules.getIsOpenPage()!=null){
			sql.append(" and m.is_open_page=?");
			param.setInt(modules.getIsOpenPage());;
		}
		return super.queryForInt(sql.toString(), param);
	}

	@Override
	public String getMaxCode(String shopId) {
		StatementParameter param = new StatementParameter();
		StringBuffer sql = new StringBuffer();
		sql.append("select max(code) from modules where 1=1 ");
		if(StringUtils.isNotBlank(shopId)){
			sql.append(" and shop_id = ? ");
			param.setString(shopId);
		}else{
			sql.append(" and shop_id is null ");
		}
		return super.queryForString(sql.toString(),param);
	}

	@Override
	public List<ModulesBO> findModules(String columnCode,String shopId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select m.id,m.name,m.goods_quantity,m.goods_exhibition_quantity,m.is_recommand_goods,m.sort,");
		sql.append("c.id 'column_id',c.name 'column_name',c.code 'column_code',m.code");
		sql.append(" from modules m");
		sql.append(" inner join `column` c on c.id=m.column_id");
		sql.append(" where m.is_delete=? ");

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());

		if (StringUtils.isNoneBlank(columnCode)) {
			sql.append(" and c.code=?");
			param.setString(columnCode);
		}
		
		if(StringUtils.isNotBlank(shopId)){
			sql.append(" and c.shop_id = ? ");
			param.setString(shopId);
		}else{
			sql.append(" and (c.shop_id is null or c.shop_id = '' ) ");
		}
		
		sql.append(" order by m.sort asc ");
		return queryForList(sql.toString(), ModulesBO.class, param);
	}
	
	@Override
	public List<Modules> getAll(){
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from modules where is_delete=? and shop_id is null");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		return queryForList(sql.toString(),Modules.class,param);
		
	}

	@Override
	public Modules getModulesByCode(String modulesCode, String shopId) {
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from modules where is_delete=? and code=?");
	
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(modulesCode);
		
		if(StringUtils.isNotBlank(shopId)){
			sql.append(" and shop_id = ? ");
			param.setString(shopId);
		}else{
			sql.append(" and shop_id is null ");
		}
		return query(sql.toString(),Modules.class,param);
	}

}
